local DIY = require "packages/diy_utility/diy_utility"
local U = require "packages/utility/utility"

local qiulong = fk.CreateSkill{
  name = "investiture__qiulong",
  tags = { DIY.ReadySkill },
}

Fk:loadTranslationTable{
  ["investiture__qiulong"] = "囚龙",
  [":investiture__qiulong"] = "蓄势技，结束阶段，你可以从游戏外获得【无锋剑】和【引蜂甲】，然后依次赠予之。",

  ["$investiture__qiulong_Void"] = "游戏外",
  ["@@investiture__qiulong"] = "囚龙",
  ["#investiture__qiulong-obtain"] = "囚龙：可以从游戏外获得【无锋剑】和【引蜂甲】。",
  ["#investiture__qiulong-present"] = "囚龙：将%arg赠予一名角色",
}

qiulong:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local need = table.every(room:getCardsByArea(Card.Void), function(id) return Fk:getCardById(id):getMark("@@investiture__qiulong") == 0 end)
  if need then
    local cards = {
      room:printCard("dull_blade", Card.Spade, 5),
      room:printCard("bee_armor", Card.Diamond, 10),
    }
    for _, c in ipairs(cards) do
      room:addCardMark(c, "@@investiture__qiulong", 1)
      room:setCardArea(c.id, Card.Void)
    end
  end
end)

qiulong:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(qiulong.name) and player == target then
      local has = table.find(player.room:getCardsByArea(Card.Void), function (id)
        return Fk:getCardById(id):getMark("@@investiture__qiulong") == 1
      end)
      return data.phase == Player.Finish and not DIY.isReadying(player, qiulong.name) and has
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = table.filter(player.room:getCardsByArea(Card.Void), function (id)
      return Fk:getCardById(id):getMark("@@investiture__qiulong") == 1
    end)
    if cards == 0 then return end
    cards = player.room:askToChooseCards(player, {
      target = player,
      flag = { card_data = { {"$investiture__qiulong_Void", cards}, }, },
      skill_name = qiulong.name,
      prompt = "#investiture__qiulong-obtain",
      min = #cards,
      max = #cards,
      cancelable = true,
    })
    if #cards == 0 then return end
    event:setCostData(self, { cards = cards })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self).cards ---@type integer[]
    local room = player.room
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, qiulong.name, nil, true, player, MarkEnum.DestructIntoDiscard)
    if player.dead then return end
    for _, id in ipairs(cards) do
      if room:getCardOwner(id) == player then
        local to = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(player, false),
          max_num = 1,
          min_num = 1,
          skill_name = qiulong.name,
          cancelable = false,
          prompt = "#investiture__qiulong-present:::" .. Fk:getCardById(id):toLogString()
        })
        U.presentCard(player, to[1], id, qiulong.name)
      end
    end
  end
})

return qiulong
